package cn.com.thingo.shyintelligent.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.th.opensdk.openapi.RemoterInfo;
import com.th.opensdk.openapi.RemoterKeyInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.thingo.shyintelligent.R;

public class DoorLockListAdapter extends RecyclerView.Adapter<DoorLockListAdapter.ViewHolder> {
    private List<RemoterInfo> list;
    private Context context;

    public DoorLockListAdapter(List<RemoterInfo> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public DoorLockListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_door_lock_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        final RemoterInfo remoterInfo = list.get(position);
        final HashMap<Integer, RemoterKeyInfo> keyList = remoterInfo.getKeyList();

        holder.tvName.setText(remoterInfo.getRemoterName());
        holder.tvType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (keyList != null && keyList.size() > 0) {
                    Set<Integer> integers = keyList.keySet();
                    for (Integer next : integers) {
                        RemoterKeyInfo remoterKeyInfo = keyList.get(next);
                        remoterInfo.sendRemoterCode(remoterKeyInfo.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_door_lock_name)
        TextView tvName;
        @BindView(R.id.item_door_lock_type)
        TextView tvType;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
